﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tilver.Data;
using Weixin.MP.DAL.WeixinMP;

namespace Weixin.MP.BLL.SysConfig
{
    public class SysConfigBO
    {
        private SysConfigMO _mo;
        public SysConfigBO()
        {
            _mo = new SysConfigMO();
        }
        
        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="eo"></param>
        /// <param name="tm"></param>
        /// <returns></returns>
        public bool Add(SysConfigEO eo, TransactionManager tm = null)
        {
            var ret = _mo.Insert(eo, tm);
            return ret > 0;
        }
        #endregion
        
        #region 查询
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="tm"></param>
        /// <returns></returns>
        public SysConfigEO Get(string key, TransactionManager tm = null)
        {
            return _mo.SelectByPK(key, tm);
        }
        #endregion
        
        #region 修改
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="tm"></param>
        /// <returns></returns>
        public bool Update(string key, string value, TransactionManager tm = null)
        {
            var sqlWhere = new StringBuilder();
            var sqlSet = new StringBuilder();
            var paras = new List<DbParameter>();
            {
                sqlWhere.Append(" [SysKey]=@SysKey ");
                var p = new SqlParameter("@SysKey", SqlDbType.VarChar);
                p.Value = key;
                paras.Add(p);
            }
            {
                sqlSet.Append(" [SysValue]=@SysValue ");
                var p = new SqlParameter("@SysValue", SqlDbType.NVarChar);
                p.Value = value;
                paras.Add(p);
            }
            var ret = _mo.Update(sqlSet.ToString().Trim(','), sqlWhere.ToString(), paras, tm);
            return ret > 0;
        }
        #endregion

    }
}
